<?php
$tmp =
<<<EOF
//版权所有(C) 2014 www.ilinei.com
if(!defined('INIT')) exit('Access Denied');

//{$table[IDENTITY]}
EOF;

$code .= $tmp;

if(count($table['FILES']) > 3){
	$tmp = 
<<<EOF

function {$table[IDENTITY]}_file_displayorder(\$a, \$b){
    if (\$a['DISPLAYORDER'] == \$b['DISPLAYORDER']) return 0;
    
    return (\$a['DISPLAYORDER'] < \$b['DISPLAYORDER']) ? -1 : 1;
}

function {$table[IDENTITY]}_file_upload_images(\$filenum){
	global \$_var;
	
	\$upload_files = array();
	
	foreach(\$_var['gp_hdnImagePath'] as \$key => \$filepath){
		if(!\$filepath) continue;
		\$filepath = substr(\$filepath, 0, 7) == 'FILEID:' ? substr(\$filepath, 7) : \$filepath;
		\$temparr = explode('|', \$filepath);
		\$temparr = get_file_stat(format_file_path(\$temparr[0]));
		\$upload_files[] = array('FILEPATH' => \$filepath, 'DISPLAYORDER' => \$_var['gp_txtImageDisplayOrder'][\$key] + 0);
		unset(\$temparr);
	}
	
	usort(\$upload_files, "{$table[IDENTITY]}_file_displayorder");
	
	\$filearr = array();
	for(\$i = 0; \$i < \$filenum; \$i++) {
		if(\$upload_files[\$i]) \$filearr['FILE'.sprintf('%02d', \$i + 1)] = \$upload_files[\$i]['FILEPATH'];
		else \$filearr['FILE'.sprintf('%02d', \$i + 1)] = '';
	}
	
	return \$filearr;
}

function {$table[IDENTITY]}_get_files(\${$table[IDENTITY]}, \$filenum){
	\${$table[IDENTITY]}_files = array();
	for(\$i = 1; \$i <= \$filenum; \$i++){
		if(is_array(\${$table[IDENTITY]}['FILE'.sprintf('%02d', \$i)])) \${$table[IDENTITY]}_files[] = \${$table[IDENTITY]}['FILE'.sprintf('%02d', \$i)];
	}
	
	return \${$table[IDENTITY]}_files;
}

EOF;
	$code .= $tmp;
}

$tmp =
<<<EOF

function {$table[IDENTITY]}_search(){
	global \$_var;
	
	\$querystring = \$wheresql = '';
	
EOF;
	
$code .= $tmp;

if($table['CODE']['CREATETIME']){
	$tmp =
<<<EOF

	if(\$_var['gp_txtBeginDate']){
		\$querystring .= '&txtBeginDate='.\$_var['gp_txtBeginDate'];
		\$wheresql .= " AND a.CREATETIME >= '{\$_var[gp_txtBeginDate]}'";
	}
	
	if(\$_var['gp_txtEndDate']){
		\$querystring .= '&txtEndDate='.\$_var['gp_txtEndDate'];
		\$wheresql .= " AND a.CREATETIME <= '{\$_var[gp_txtEndDate]}'";
	}
	
EOF;
	
	$code .= $tmp;
}elseif($table['CODE']['USER']){
	$tmp =
<<<EOF

	if(\$_var['gp_txtBeginDate']){
		\$querystring .= '&txtBeginDate='.\$_var['gp_txtBeginDate'];
		\$wheresql .= " AND a.EDITTIME >= '{\$_var[gp_txtBeginDate]}'";
	}
	
	if(\$_var['gp_txtEndDate']){
		\$querystring .= '&txtEndDate='.\$_var['gp_txtEndDate'];
		\$wheresql .= " AND a.EDITTIME <= '{\$_var[gp_txtEndDate]}'";
	}
	
EOF;
	
	$code .= $tmp;
}

if(count($table['KEYWORDS']) > 0){
	$keywords = array();
	
	foreach ($table['KEYWORDS'] as $key => $val){
		$keywords[] = $val['key'];
	}
	
	$concat = 'CONCAT('.implode(', ', $keywords).')';
	
	$tmp =
<<<EOF

	if(\$_var['gp_txtKeyword']){
		\$_var['gp_txtKeyword'] = trim(\$_var['gp_txtKeyword']);
		\$querystring .= '&txtKeyword='.\$_var['gp_txtKeyword'];
		
		\$_var['gp_sltType'] = \$_var['gp_sltType'] + 0;
		
		if(\$_var['gp_sltType'] == 0) {
			\$wheresql .= " AND {$concat} LIKE '%{\$_var[gp_txtKeyword]}%'";
		}
EOF;
	
	$code .= $tmp;
	
	foreach ($table['KEYWORDS'] as $key => $val){
		$num = $key + 1;
		$tmp =
<<<EOF
elseif(\$_var['gp_sltType'] == {$num}){
			\$wheresql .= " AND {$val[key]} LIKE '%{\$_var[gp_txtKeyword]}%'";
		}
EOF;
		$code .= $tmp;
		unset($num);
	}
	
	$tmp =
<<<EOF

	}
	
EOF;
	
	$code .= $tmp;
}

if($table['CODE']['DISTRICT']){
	$tmp =
<<<EOF

	if(\$_var['gp_sltSProvinceId']){
		\$querystring .= '&sltSProvinceId='.\$_var['gp_sltSProvinceId'];
		\$wheresql .= " AND a.PROVINCEID  = '{\$_var[gp_sltSProvinceId]}'";
	}
	
	if(\$_var['gp_sltSCityId']){
		\$querystring .= '&sltSCityId='.\$_var['gp_sltSCityId'];
		\$wheresql .= " AND a.CITYID  = '{\$_var[gp_sltSCityId]}'";
	}
	
	if(\$_var['gp_sltSCountyId']){
		\$querystring .= '&sltSCountyId='.\$_var['gp_sltSCountyId'];
		\$wheresql .= " AND a.COUNTYID  = '{\$_var[gp_sltSCountyId]}'";
	}
	
EOF;
	
	$code .= $tmp;
}

if($table['CODE']['STATUS']){
	$tmp =
<<<EOF

	if(\$_var['gp_sltStatus']){
		\$querystring .= '&sltStatus='.\$_var['gp_sltStatus'];
		if(\$_var['gp_sltStatus'] == 1) \$wheresql .= " AND a.STATUS  = 1";
		elseif(\$_var['gp_sltStatus'] == 2) \$wheresql .= " AND a.STATUS  = 0";
	}
	
EOF;
	
	$code .= $tmp;
}elseif($table['CODE']['ISAUDIT']){
	$tmp =
<<<EOF

	if(\$_var['gp_sltIsAudit']){
		\$querystring .= '&sltIsAudit='.\$_var['gp_sltIsAudit'];
		if(\$_var['gp_sltIsAudit'] == 1) \$wheresql .= " AND a.ISAUDIT  = 1";
		elseif(\$_var['gp_sltIsAudit'] == 2) \$wheresql .= " AND a.ISAUDIT  = 0";
	}
	
EOF;
	
	$code .= $tmp;
}else

foreach ($table['JOINS'] as $key => $join){
	if($key == 'user' || $key == 'category') continue;
	
	if($key == 'group'){
		$code .= "
	if(\$_var['gp_sltSGroupID']) {
		\$querystring .= '&sltSGroupID='.\$_var['gp_sltSGroupID'];
		\$wheresql .= \" AND a.GROUPID  = '{\$_var[gp_sltSGroupID]}'\";
	}
	";
		continue;
	}
	
	$name = strtolower($key);
	$name = strtoupper(substr($name, 0, 1)).substr($name, 1);
	$name_upper = strtoupper($key);
	
	$code .= "
	if(\$_var['gp_sltS{$name}ID']) {
		\$querystring .= '&sltS{$name}ID='.\$_var['gp_sltS{$name}ID'];
		\$wheresql .= \" AND a.{$name_upper}ID  = '{\$_var[gp_sltS{$name}ID]}'\";
	}
	";
}

$tmp =
<<<EOF

	if(\$_var['gp_hdnSearchShow']) \$querystring .= '&hdnSearchShow='.\$_var['gp_hdnSearchShow'];
	if(\$_var['gp_nextquery']) \$querystring .= '&nextquery='.\$_var['gp_nextquery'];
	
	return array('querystring' => \$querystring, 'wheresql' => \$wheresql);
}

EOF;

$code .= $tmp;

if($table['CODE']['STATUS']){
	$tmp =
<<<EOF

function {$table[IDENTITY]}_get_status(){
	return array(
	0 => '未启用', 
	1 => '已启用'
	);
}

EOF;
	
	$code .= $tmp;
}

$tmp =
<<<EOF

function {$table[IDENTITY]}_get_by_id(\${$table[IDENTITY]}id){
	global \$db;
	
	return \$db->fetch_first("SELECT * FROM tbl_{$table[IDENTITY]} WHERE {$table[PRIMARY_KEY]} = '{\${$table[IDENTITY]}id}'");
}

function {$table[IDENTITY]}_get_count(\$wheresql = ''){
	global \$db;
	
	return \$db->result_first("SELECT COUNT(1) FROM tbl_{$table[IDENTITY]} a WHERE 1 {\$wheresql}") + 0;
}

function {$table[IDENTITY]}_get_list(\$start , \$perpage, \$wheresql = '', \$ordersql = ''){
	global \$db;
	
	!\$ordersql && \$ordersql = "ORDER BY a.{$table[ORDER_KEY]} DESC";
	\$perpage > 0 && \$limitsql = "LIMIT \$start, \$perpage";
	
	\$rows = array();
	\$temp_query = \$db->query("SELECT a.* FROM tbl_{$table[IDENTITY]} a WHERE 1 {\$wheresql} {\$ordersql} {\$limitsql}");
	while(\$row = \$db->fetch_array(\$temp_query)){
		\$row = format_row_files(\$row);
		\$rows[] = \$row;
	}
	
	return \$rows;
}

function {$table[IDENTITY]}_get_all(\$wheresql = '', \$ordersql = ''){
	global \$db;
	
	!\$ordersql && \$ordersql = "ORDER BY a.{$table[ORDER_KEY]} DESC";
	
	\$rows = array();
	\$temp_query = \$db->query("SELECT a.* FROM tbl_{$table[IDENTITY]} a WHERE 1 {\$wheresql} {\$ordersql}");
	while(\$row = \$db->fetch_array(\$temp_query)){
		\$row = format_row_files(\$row);
		\$rows[] = \$row;
	}
	
	return \$rows;
}

function {$table[IDENTITY]}_insert(\$data){
	global \$db;
	
	\$db->insert('tbl_{$table[IDENTITY]}', \$data);
	
	return \$db->insert_id();
}

function {$table[IDENTITY]}_update(\$id, \$data){
	global \$db;

	\$db->update('tbl_{$table[IDENTITY]}', \$data, "{$table[PRIMARY_KEY]} = '{\$id}'");
}
EOF;

$code .= $tmp;
?>